package com.wl.spring;

import com.wl.spring.biz.UserBiz;
import com.wl.spring.mapper.UserMapper;
import com.wl.spring.web.UserAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;

//注解 jvm 读取的信息
@Configuration  //相当于java语句版的beans.xml
@ComponentScan("com.wl.spring")
public class AnnoConfig {

    @Bean(initMethod="init",destroyMethod="destroy")
    @Lazy
    @Scope("prototype") //原型模式
    public Demo demo() {
        Demo demo = new Demo();
        demo.setName("张三");
        return demo;
    }

    @Bean(initMethod="init",destroyMethod="destroy")
    public Demo newDemo1(){
        Demo demo = new Demo();
        demo.setName("王五");
        return demo;
    }

    @Bean(initMethod="init",destroyMethod="destroy")
    @Primary //默认 bean
    public Demo newDemo(){
        Demo demo = new Demo();
        demo.setName("李四");
        return demo;
    }

    @Bean
    public UserMapper userMapper() {
        return new UserMapper();
    }

    //@Autowired 表示从IOC 容器中获取该 Bean 注入到该属性中
    @Autowired
    UserMapper  userMapper;
    @Bean
    public UserBiz userBiz() {
        UserBiz userBiz = new UserBiz();
        userBiz.setUserMapper(userMapper());
        return userBiz;
    }

    //bean 方法参数注入对象，Spring 会从IOC容器获取一个对应bean 注入进来
    //这个叫"接口（方法）注入"
    @Bean
    //                         通过方法参数注入userbiz
    public UserAction userAction(UserBiz userBiz) {
        UserAction userAction = new UserAction();
        userAction.setUserBiz(userBiz);
        return userAction;
    }


}
